package com.af.security.mgt.operation;

import com.af.system.util.AfStringUtils;
import com.github.yitter.idgen.YitIdHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : zhenyun.su
 * @since : 2018/9/17
 */

@Service
public class OperationService {

    @Autowired
    private OperationRepository operationRepository;

    public List<Operation> getOperationsByIds(String ids){
        Iterable<Long> IdList =  AfStringUtils.asLongList(ids,",");
        if (IdList !=null){
            return operationRepository.findAllById(IdList);
        }else{
            return null;
        }
    }

    public List<Operation> getAll(){
        return operationRepository.findAll();
    }

    @Transactional(rollbackFor=Exception.class)
    public void saveAll(List<Operation> operations){
        operationRepository.saveAll(operations);
    }

    @Transactional(rollbackFor=Exception.class)
    public void save(Operation operation){
        if (operation.getId() != null){
            operation.setId(YitIdHelper.nextId());
        }
        operationRepository.save(operation);
    }
}
